#!/usr/bin/env python
# $Id: test2-54.py acd96138aeae 2013/07/04 08:51:14 pgurumur $
# Copyright (c) 2013 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# File test2-54.py generated on Tuesday March 12, 2013 @23:18:42 PDT
#
__version__  = "$Revision: acd96138aeae $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2013-07-04 08:51:14Z $"

import sys
from urllib2 import urlopen
import json
from xml.dom import minidom
import xml.parsers.expat

# 3 handler functions
def start_element(name, attrs):
   print(name)
def end_element(name):
   pass
def char_data(data):
   print 'Character data:', repr(data)



if __name__ == "__main__":
   """
   apikey = "e037c18a-33aa-4427-abb0-e497009939d2"
   http://www.dictionaryapi.com/api/v1/references/thesaurus/xml/test?key=44842826-20b5-41d6-81a8-cd25a2d443b4
   url = "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/test?key=e037c18a-33aa-4427-abb0-e497009939d2"
    htotp://www.dictionaryapi.com/api/v1/references/collegiate/xml/hypocrite?key=[YOUR KEY GOES HERE]
   apikey = "e037c18a-33aa-4427-abb0-e497009939d2"
   word = sys.argv[1]
   url = "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/"
   url += "%s?key=%s" %(word, apikey)

   handle = urlopen(url)
   print(handle.read())

   xmldoc = minidom.parse('test.xml')
   itemlist = xmldoc.getElementsByTagName('entry') 
   for ix in xrange(len(itemlist)):
   print itemlist[0].attributes['id'].value
   for s in itemlist:
      print s.attributes['name'].value
      print itemlist[ix].attribute['id'].value
   print itemlist[0].attributes['id'].value
   """

   p = xml.parsers.expat.ParserCreate() 
   p.StartElementHandler = start_element
   p.EndElementHandler = end_element
   p.CharacterDataHandler = char_data

   inputfile = "test.xml"
   with open(inputfile, "r") as fh:
      p.Parse(fh.read(), 1)
